<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>String 1</title>
</head>

<body>

</body>

</html>
<script>
    // 计算每个字符出现次数
    let srt1 = prompt("请输入字符串");
    console.log(srt1);
    function countChar(str) {
        let charCount = {};
        for (let i = 0; i < str.length; i++) {
            const char = str[i];
            if (charCount[char]) {
                charCount[char]++;
            } else {
                charCount[char] = 1;
            }
        }
       //将字符计数对象转化为可读的字符串
       let resultStr = '';
       for(const char in charCount) {
        resultStr += `${char}: ${charCount[char]}\n`;
       }
       return resultStr;
    }
    let result = countChar(srt1);
    alert(result)
    
</script>